package com.sky.Aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.xml.stream.events.EntityReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * ClassName: Ascept
 * Package: com.sky
 * Description:
 *
 * @Author BuTang
 * @Create 2023/10/24 17:26
 * @Version 1.0
 */
@Slf4j
@Aspect
@Component
public class AutoFillAspect {

    /**
     * 切入点
     * [* com.sky.mapper.*.*(..)] 任意返回值，com.sky.mapper 所有所有类，所有方法，所有形参
      */
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCute(){}


    @Before("autoFillPointCute()")
    public void autoFill(JoinPoint joinPoint) {
        //根据反射获取方法的annotation
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AutoFill annotation = signature.getMethod().getAnnotation(AutoFill.class);
        OperationType operationType = annotation.value();

        //获取方法的参数
        Object[] args = joinPoint.getArgs();

        if (args == null && args.length ==0){
            return;
        }

        Object entity = args[0];

        LocalDateTime nowTime = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();

        //when type = insert
        if (operationType == OperationType.INSERT) {
            try {
                //通过反射获取方法
                Method createUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method createTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method updateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                Method updateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);

                createUser.invoke(entity,currentId);
                createTime.invoke(entity,nowTime);
                updateUser.invoke(entity,currentId);
                updateTime.invoke(entity,nowTime);
            } catch (Exception e) {
                //栈堆跟踪
                e.printStackTrace();
            }
            return;
        }else if (operationType == OperationType.UPDATE){

            try {
                Method updateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                Method updateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                updateUser.invoke(entity,currentId);
                updateTime.invoke(entity,nowTime);
            } catch (Exception e) {
                e.printStackTrace();
            }}
    }
}
